package com.jk.youxuanmall.member.controller;

import java.util.Arrays;

import com.jk.youxuanmall.common.pojo.PagedResponse;
import com.jk.youxuanmall.common.pojo.ResponseResult;
import com.jk.youxuanmall.member.feign.CouponSeviceFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.jk.youxuanmall.member.entity.MemberEntity;
import com.jk.youxuanmall.member.service.MemberService;



/**
 * 会员主表
 *
 * @author yangzhichao
 * @email 780979437@qq.com
 * @date 2025-07-29 10:44:05
 */
@RestController
@RequestMapping("member/member")
public class MemberController {
    @Autowired
    private MemberService memberService;

    @Autowired
    private CouponSeviceFeign couponSeviceFeign;

    @GetMapping(value = "findCouponListByUsername")
    public ResponseResult findCouponListByUsername() {
        return couponSeviceFeign.findCouponByUsername("张三");
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public ResponseResult<PagedResponse<MemberEntity>> list(MemberEntity memberEntity, Integer pageNum, Integer pageSize){
        return new ResponseResult<>(200, "查询成功!", memberService.queryPage(memberEntity, pageNum, pageSize));
    }


    /**
     * 信息回显
     */
    @RequestMapping("/info/{id}")
    public ResponseResult<MemberEntity> info(@PathVariable("id") Long id){
		MemberEntity member = memberService.getById(id);

        return new ResponseResult<>(200, "查询成功！", member);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public ResponseResult save(@RequestBody MemberEntity member){
        try {
            memberService.save(member);
            return new ResponseResult(200, "新增成功！");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult(444, e.getMessage());
        }


    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public ResponseResult update(@RequestBody MemberEntity member){
        try {
            memberService.updateById(member);
            return new ResponseResult(200, "修改成功！");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult(444, "修改失败！");
        }
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public ResponseResult delete(@RequestBody Long[] ids){
        try {
            memberService.removeByIds(Arrays.asList(ids));
            return new ResponseResult(200, "删除成功！");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult(444, "删除失败！");
        }
    }

}
